package com.fasttreatment.zzr.demo.activity;

import com.android.androidlibrary.base.BaseActivity;

import butterknife.BindView;

import android.Manifest;
import android.content.Intent;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.fasttreatment.zzr.demo.R;
import com.fasttreatment.zzr.demo.fragment.Test1Fragment;
import com.fasttreatment.zzr.demo.fragment.Test2Fragment;
import com.fasttreatment.zzr.demo.fragment.Test3Fragment;
import com.fasttreatment.zzr.demo.model.NetEvent;
import com.fasttreatment.zzr.demo.model.UserRealm;
import com.fasttreatment.zzr.demo.myrouter.UrlRouter;
import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationItem;
import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationView;
import com.luseen.luseenbottomnavigation.BottomNavigation.OnBottomNavigationItemClickListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

public class MainActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
    @BindView(R.id.bottomNavigation)
    BottomNavigationView mBottomNavigation;
    @BindView(R.id.toolbar_right_btn)
    Button mToolbarRightBtn;
    @BindView(R.id.toolbar_title_tv)
    TextView mToolbarTitleBar;

    private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;

    private Test1Fragment mTest1Fragment;
    private Test2Fragment mTest2Fragment;
    private Test3Fragment mTest3Fragment;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mFragmentTransaction;

    private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1;
    public static final String MESSAGE_RECEIVED_ACTION = "com.fasttreatment.zzr.demo.MESSAGE_RECEIVED_ACTION";
    // private MessageReceiver mMessageReceiver;
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";
    public static boolean isForeground = false;
    private int texts[] = {R.string.bottom_navigation_gank, R.string.bottom_navigation_2, R.string.bottom_navigation_3};

    //加载布局
    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    //初始化数据
    @Override
    public void initData(Bundle savedInstanceState) {
        mFragmentManager = this.getSupportFragmentManager();
        initFragment();
        initBottomNavigation();
    }

    //绑定事件
    @Override
    public void setListener() {
        mBottomNavigation.setOnBottomNavigationItemClickListener(new OnBottomNavigationItemClickListener() {
            @Override
            public void onNavigationItemClick(int index) {
                setFragment(index);
            }
        });

        mToolbarRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UrlRouter.goQrcode(context);
            }
        });
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String result = bundle.getString("result");
            Toast.makeText(context, result + "---" + resultCode, Toast.LENGTH_SHORT).show();
        }
    }

    //初始化fragment
    public void initFragment() {
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mTest1Fragment = new Test1Fragment();
        mTest2Fragment = new Test2Fragment();
        mTest3Fragment = new Test3Fragment();

        mFragmentTransaction.add(R.id.layFrame, mTest1Fragment, "mTest1Fragment");
        mFragmentTransaction.add(R.id.layFrame, mTest2Fragment, "mTest2Fragment");
        mFragmentTransaction.add(R.id.layFrame, mTest3Fragment, "mTest3Fragment");

        mFragmentTransaction.commit();

        setFragment(0);
    }

    //设置fragment显示
    public void setFragment(int index) {
        mFragmentTransaction = mFragmentManager.beginTransaction();
        setHideFragment();
        switch (index) {
            case 0:
                mFragmentTransaction.show(mTest1Fragment);
                mToolbarTitleBar.setText("首页");
                mToolbarRightBtn.setVisibility(View.VISIBLE);
                break;
            case 1:
                mFragmentTransaction.show(mTest2Fragment);
                mToolbarTitleBar.setText("列表");
                mToolbarRightBtn.setVisibility(View.GONE);
                break;
            case 2:
                mFragmentTransaction.show(mTest3Fragment);
                mToolbarTitleBar.setText("Test3");
                mToolbarRightBtn.setVisibility(View.GONE);
                break;
        }
        mFragmentTransaction.commitAllowingStateLoss();
    }

    //默认隐藏
    public void setHideFragment() {
        mFragmentTransaction.hide(mTest3Fragment);
        mFragmentTransaction.hide(mTest2Fragment);
        mFragmentTransaction.hide(mTest1Fragment);
    }

    //初始化底部tab
    public void initBottomNavigation() {
        for (int i = 0; i < texts.length; i++) {
            BottomNavigationItem gankItem = new BottomNavigationItem
                    (getResources().getString(texts[i]), ContextCompat.getColor(context, R.color.colorActive), R.mipmap.ic_launcher);
            mBottomNavigation.addTab(gankItem);
        }

        mBottomNavigation.isColoredBackground(false);
        mBottomNavigation.setItemActiveColorWithoutColoredBackground(R.color.colorInactive);


    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @AfterPermissionGranted(REQUEST_CODE_QRCODE_PERMISSIONS)
    private void requestCodeQRCodePermissions() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, "扫描二维码需要打开相机和散光灯的权限", REQUEST_CODE_QRCODE_PERMISSIONS, perms);
        }
    }

    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        requestCodeQRCodePermissions();
    }

    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        //unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(String messageEvent) {
        Toast.makeText(context, messageEvent, Toast.LENGTH_SHORT).show();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getNet(NetEvent netEvent) {
        Log.i("zzzzzzzzz",netEvent.getState()+"");
    }

    @Override
    public boolean useEventBus() {
        return true;
    }
}
